package com.woniuxy.social.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.life.core.api.CommonResult;
import com.woniuxy.life.core.api.SocialConstants;
import com.woniuxy.social.entity.Community;
import com.woniuxy.social.mapper.CommunityMapper;
import com.woniuxy.social.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 社区用户表 服务实现类
 * </p>
 *
 * @author John_Yeung
 * @since 2020-09-24
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Autowired
    CommunityMapper communityMapper;

    @Override
    public CommonResult updateCommunityName(Community community) {
        System.err.println(community);
        Integer communityId = community.getCommunityId();
        if (communityId==null){
            return CommonResult.failed("请登录后再做修改");
        }
        String communityName = community.getCommunityName();
        if (communityName==null&&communityName.length()<1){
            return CommonResult.failed("昵称至少长度至少为1");
        }
        Community com = communityMapper.selectById(communityId);
        com.setCommunityName(communityName);
        try {
            communityMapper.updateById(com);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改昵称失败");
        }
        return CommonResult.success("昵称修改成功");
    }

    @Override
    public CommonResult updateCommunityTag(Community community) {
        Integer communityId = community.getCommunityId();
        if (communityId==null){
            return CommonResult.failed("请登录后再做修改");
        }
        String communityTag = community.getCommunityTag();
        Community com = communityMapper.selectById(communityId);
        com.setCommunityTag(communityTag);
        try {
            communityMapper.updateById(com);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("签名修改失败");
        }
        return CommonResult.success("签名修改成功");
    }

    @Override
    public CommonResult communityRegister(Integer communityId) {
        if (communityId==null){
            return CommonResult.failed("请登录后再签到");
        }
        Community community = communityMapper.selectById(communityId);

        Integer speakState = community.getSpeakState();
        if(speakState!=SocialConstants.COMMUNITY_SPEAK_ABLE){
            community.setRegisterDay(0);
            communityMapper.updateById(community);
            return CommonResult.failed("你的账号处于限制状态，签到无法获取积分");
        }

        Integer registerDay = community.getRegisterDay();
        Integer gobal = community.getGobal();
        Integer getGobal=0;
        if (registerDay<SocialConstants.COMMUNITY_REGISTER_A){
            getGobal=SocialConstants.COMMUNITY_GOBAL_A;
            gobal+=SocialConstants.COMMUNITY_GOBAL_A;
        }
        else if (registerDay<SocialConstants.COMMUNITY_REGISTER_B){
            getGobal=SocialConstants.COMMUNITY_GOBAL_B;
            gobal+=SocialConstants.COMMUNITY_GOBAL_B;
        }
        else if (registerDay<SocialConstants.COMMUNITY_REGISTER_C){
            getGobal=SocialConstants.COMMUNITY_GOBAL_C;
            gobal+=SocialConstants.COMMUNITY_GOBAL_C;
        }
        else{
            getGobal=SocialConstants.COMMUNITY_GOBAL_D;
            gobal+=SocialConstants.COMMUNITY_GOBAL_D;
        }
        community.setGobal(gobal);
        registerDay+=1;
        community.setRegisterDay(registerDay);

        LocalDateTime lastRegister = community.getThisRegister();
        community.setLastRegister(lastRegister);
        community.setThisRegister(LocalDateTime.now());

        try {
            communityMapper.updateById(community);
        }
        catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("签到失败，请反馈后台");
        }

        return CommonResult.success("签到成功，你已签到"+registerDay+"天，获得"+getGobal+"积分");
    }

    @Transactional
    @Override
    public CommonResult insertCommunity(Integer userId,String communityName) {
        QueryWrapper<Community> wrapper=new QueryWrapper<Community>();
        wrapper.eq("USER_ID",userId);
        Community findcommunity = communityMapper.selectOne(wrapper);
        if (findcommunity!=null){
            return CommonResult.failed("创建失败，该账户下已有社区账户");
        }
        if (communityName==null||communityName.length()<1){
            return CommonResult.failed("社区用户昵称长度必须大于1");
        }
        Community community=new Community();
        community.setUserId(userId);
        community.setCommunityName(communityName);
        community.setLastRegister(LocalDateTime.now());
        community.setThisRegister(LocalDateTime.now());
        community.setGobal(0);
        community.setRegisterDay(0);
        community.setCreateDate(LocalDateTime.now());
        community.setFlag(0);
        community.setGetGobalTime(5);
        community.setKidness(100);
        community.setSpeakState(0);
        community.setSpeakTime(128);
        try {
            communityMapper.insert(community);
        }
        catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("社区账户添加成功");
        }
        return CommonResult.success("社区账户添加成功");
    }
}
